//2025-09-10 10:19:05 动态计算查询结果脚本

// 从上下文中获取所有可用的关系
var relationships = context.getAvailableRelationships();
var REL_SUCCESS = null;
var REL_FAILURE = null;

relationships.forEach(function (rel) {
    if (rel.getName() === 'success') {
        REL_SUCCESS = rel;
    } else if (rel.getName() === 'failure') {
        REL_FAILURE = rel;
    }
});

var flowFile = session.get();
if (flowFile === null) {
    flowFile = session.create();
}

try {
    if (REL_SUCCESS === null) {
        throw new Error("在处理器的可用关系中找不到 'success' 关系。");
    }

    var currentDate = java.time.LocalDate.now().toString();

    var jsonData = {
        "applyDateStart": currentDate, "applyDateEnd": currentDate, "initAccountList": "591915131310106"
    };

    var jsonString = JSON.stringify({
        "applyDateEnd": "2025-09-10",
        "applyDateStart": "2025-09-01",
        "currentPage": 1,
        "initAccountList": [
            "755930515810902",
            "755930515810504",
            "755930515910501",
            "655905707410000",
            "655905707110000"
        ],
        "pageSize": 50
    });

    flowFile = session.write(flowFile, new (Java.type('org.apache.nifi.processor.io.OutputStreamCallback'))({
        process: function (outputStream) {
            outputStream.write(jsonString.getBytes("UTF-8"));
        }
    }));

    session.transfer(flowFile, REL_SUCCESS);
} catch (e) {
    log.error('处理 JSON 并创建 FlowFile 时出错。', e);
    if (REL_FAILURE !== null) {
        session.transfer(flowFile, REL_FAILURE);
    } else {
        session.remove(flowFile);
    }
}